package io.zhengsh.order.bean;

import io.zhengsh.order.annotaion.Anno;
import io.zhengsh.order.annotaion.AnnoAspect;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.core.MethodIntrospector;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@EnableAspectJAutoProxy
@Import({AnnoAspect.class, TestBean.class})
public class BeanStart {

    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanStart.class);
        TestBean bean = applicationContext.getBean(TestBean.class);

        Class<? extends TestBean> classz = bean.getClass();
        Method[] methods = classz.getMethods();

//        Map<Method, T> methods = MethodIntrospector.selectMethods(classz,
//                (MethodIntrospector.MetadataLookup<T>) method -> {
//                    try {
//                        return getMappingForMethod(method, userType);
//                    }
//                    catch (Throwable ex) {
//                        throw new IllegalStateException("Invalid mapping on handler class [" +
//                                userType.getName() + "]: " + method, ex);
//                    }
//                });

        Anno ao = AnnotationUtils.findAnnotation(classz, Anno.class);


        for (Method m : methods) {
            Anno annotation = m.getAnnotation(Anno.class);

            if (annotation != null) {
                System.out.println(" ============= invoke test ===========");
                m.invoke(bean, new Object());
            }

            Anno annotation1 = AnnotationUtils.findAnnotation(m, Anno.class);
            if (annotation1 != null) {
                System.out.println(" ============= invoke test1111 ===========");
                m.invoke(bean);
            }
        }
        System.out.println(" ============= 分割线 ===========");
        bean.test();
    }
}
